Next: Regexps, Previous: Symbol Search, Up: Search [Contents][Index]
A regular expression (or regexp for short) is a pattern that denotes a class of alternative strings to match. Emacs provides both incremental and nonincremental ways to search for a match for a regexp. The syntax of regular expressions is explained in the next section.
Begin incremental regexp search
(isearch-forward-regexp).
Begin reverse incremental regexp search
(isearch-backward-regexp).
Incremental search for a regexp is done by typing
C-M-s (isearch-forward-regexp), by
invoking C-s with a prefix argument (whose value does
not matter), or by typing M-r within a forward
incremental search. This command reads a search string
incrementally just like C-s, but it treats the search
string as a regexp rather than looking for an exact match against
the text in the buffer. Each time you add text to the search
string, you make the regexp longer, and the new regexp is
searched for. To search backward for a regexp, use
C-M-r (isearch-backward-regexp),
C-r with a prefix argument, or M-r within a
backward incremental search.
All of the special key sequences in an ordinary incremental
search (see Special Isearch) do
similar things in an incremental regexp search. For instance,
typing C-s immediately after starting the search
retrieves the last incremental search regexp used and searches
forward for it. Incremental regexp and non-regexp searches have
independent defaults. They also have separate search rings, which
you can access with M-p and M-n. The
maximum number of search regexps saved in the search ring is
determined by the value of regexp-search-ring-max,
16 by default.
Unlike ordinary incremental search, incremental regexp search
does not use lax space matching by default. To toggle this
feature use M-s SPC
(isearch-toggle-lax-whitespace). Then any SPC typed in incremental regexp search will match any
sequence of one or more whitespace characters. The variable
search-whitespace-regexp specifies the regexp for
the lax space matching. See Special Isearch.
Also unlike ordinary incremental search, incremental regexp search cannot use character folding (see Lax Search). (If you toggle character folding during incremental regexp search with M-s ', the search becomes a non-regexp search and the search pattern you typed is interpreted as a literal string.)
In some cases, adding characters to the regexp in an incremental regexp search can make the cursor move back and start again. For example, if you have searched for ‘foo’ and you add ‘\|bar’, the cursor backs up in case the first ‘bar’ precedes the first ‘foo’. See Regexps.
Forward and backward regexp search are not symmetrical, because regexp matching in Emacs always operates forward, starting with the beginning of the regexp. Thus, forward regexp search scans forward, trying a forward match at each possible starting position. Backward regexp search scans backward, trying a forward match at each possible starting position. These search methods are not mirror images.
Nonincremental search for a regexp is done with the commands
re-search-forward and
re-search-backward. You can invoke these with
M-x, or by way of incremental regexp search with
C-M-s RET and C-M-r
RET. When you invoke these
commands with M-x, they search for the exact regexp
you specify, and thus don’t support any lax-search features
(see Lax Search) except
case folding.
If you use the incremental regexp search commands with a
prefix argument, they perform ordinary string search, like
isearch-forward and isearch-backward.
See Incremental
Search.
Next: Regexps, Previous: Symbol Search, Up: Search [Contents][Index]